package org.example.springboot_threadpool_demo;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Service
public class AsyncService {

    // 默认去找beanName为taskExecutor的，没有则找实现类
    @Async
    public void asyncMethod() {
        try {
            TimeUnit.SECONDS.sleep(2);
            System.out.println("asyncMethod threadName:" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Async("taskExecutor2")
    public void asyncMethod2() {
        try {
            TimeUnit.SECONDS.sleep(2);
            System.out.println("asyncMethod2 threadName:" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Async
    public Future<Integer> asyncMethod3() {
        try {
            System.out.println("asyncMethod3 threadName:" + Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(3);
            return AsyncResult.forValue(123);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
